package pers.xiaojun.boot.sprindoc.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import pers.xiaojun.boot.sprindoc.config.properties.SpringDocProperties;

import java.util.Map;

/**
 * Swagger 自动配置类
 *
 * @author xiaojun
 * @since 2025-10-23
 */
@AutoConfiguration
@RequiredArgsConstructor
@EnableConfigurationProperties(SpringDocProperties.class)
public class SpringDocAutoConfiguration {

    private final SpringDocProperties properties;

    /**
     * 全局配置
     */
    @Bean
    public OpenAPI createOpenAPI() {
        OpenAPI openAPI = new OpenAPI()
                .info(buildInfo());
        return openAPI;
    }

    /**
     * API 摘要信息
     */
    private Info buildInfo() {
        return new Info()
                .title(properties.getTitle())
                .description(properties.getDescription())
                .version(properties.getVersion())
                .contact(new Contact()
                        .name(properties.getAuthor().getName())
                        .url(properties.getAuthor().getUrl())
                        .email(properties.getAuthor().getEmail()))
                .license(new License()
                        .name(properties.getLicense())
                        .url(properties.getLicenseUrl())
                );
    }

    @Bean
    public GroupedOpenApi siteApi() {
        return GroupedOpenApi.builder()
                .group("管理后台接口")
                .pathsToMatch("/admin-api/**")
                .build();
    }

    @Bean
    public GroupedOpenApi adminApi() {
        return GroupedOpenApi.builder()
                .group("应用服务接口")
                .pathsToMatch("/app-api/**")
                .build();
    }

    @Bean
    public GroupedOpenApi commonApi() {
        return GroupedOpenApi.builder()
                .group("公共接口")
                .pathsToMatch("/api/**")
                .build();
    }


}
